###############################################                                     
# Cause of Effect? Turnout in Hispanic Majority-Minority Districts
#  - John A. Henderson, Jasjeet S. Sekhon, and Rocio Titiunik
#  - Forthcoming in Political Analysis
#  - Replication file for <Figure 2b>
#  - April 14, 2016
###############################################

options(width=150)
rm(list=ls())   

path='/local/'  
source(paste(path,'replicationPA/funs/headers.R',sep=''))  

# Figure 2(b) Florida, 2010 Hispanic Registration   
load(paste(path,'replicationPA/Figure2/FL_final_data_merged.Rdata',sep=''))
                   
data2010=data2010[order(data2010[,2]),]

sum(abs((cdold[,1]-data2010[,2])+(cdnew[,1]-data2010[,2])))==0
sum(adnew[,1]-data2010[,2])
sum(sdnew[,1]-data2010[,2])

abs(sum(cdold[,2]-data2010[,9]))==0
abs(sum(cdnew[,2]-data2010[,9]))==0 

cd10=cdnew
ad10=adnew
sd10=sdnew
        
names(data2010)[9:11]=c('cd00','sd00','ad00')
                              
data2010=cbind(data2010,cd10[,2],sd10[,2],ad10[,2])
  
names(data2010)[65:67]=c('cd10','sd10','ad10')
     
# hispanic districts in FL pre and post 2010 redistricting 
baseold=c(18, 21, 25)
basenew=c(6, 25, 26, 27)

hisp10=hisp00=array(0,nrow(data2010))

for(j in 1:length(baseold)){
	ix=which(data2010$cd00==baseold[j])
	hisp00[ix]=1
}
for(j in 1:length(basenew)){
	ix=which(data2010$cd10==basenew[j])
	hisp10[ix]=1
}
     
# defining treatment 
Tr=hisp10-hisp00
Tr[which(hisp00==1)]=NA

yhispReg=data2010$REG_HISP/data2010$VAP_H
yhispReg[which(as.numeric(as.character(data2010$VAP_H))==0)]=NA
# serious overruns due to mismatch of census population estimates and registration/turnout data
yhispReg[which(yhispReg>1)]=NA 

# matching on phvap
x=data2010$VAP_H/data2010$VAP
                                                      
set.seed(1005)                   
indxna=which(!is.na(Tr) & !is.na(x))   

use.matching=F
if(use.matching==T){ 
	# note: this can take a bit of time due to high # of blocks 
	mout=Match(Tr=Tr[indxna],X=x[indxna],ties=F)
} else{
	load(paste(path,'replicationPA/Figure2/mout2010-FL.Rdata',sep=''))
}   

tr_mout=Tr[indxna][c(mout$index.treated,mout$index.control)]
yhispReg_mout=yhispReg[indxna][c(mout$index.treated,mout$index.control)]

pdf(file=paste(path,'replicationPA/Figure2/conditionRegBias2010-FL.pdf',sep=''))
qqplot(yhispReg_mout[which(tr_mout==1)],yhispReg_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhispReg_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhispReg_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
	rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()   
                 
png(file=paste(path,'replicationPA/Figure2/conditionRegBias2010-FL.png',sep=''),res=65)
	qqplot(yhispReg_mout[which(tr_mout==1)],yhispReg_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
  	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhispReg_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhispReg_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
	 rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()

# End Figure2b.R